package com.jwdf.uda.selector.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.databinding.ItemImageSelectorAddBinding;
import com.jwdf.uda.databinding.ItemImageSelectorBinding;
import com.jwdf.uda.selector.ImageSelectorItem;
import com.jwdf.uda.ui.adapter.AbsAdapter;
import com.jwdf.uda.ui.adapter.AbsViewHolder;
import com.jwdf.uda.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：
 */
public class ImageSelectorAdapter extends AbsAdapter<ImageSelectorItem> {
    private final static int ITEM_IMAGE = 0;
    private final static int ITEM_ADD = 1;

    private int mItemWidth;

    public ImageSelectorAdapter(Context context, int itemWidth) {
        super(context);
        mItemWidth = itemWidth;
    }

    public void defaultInit(){
        List<ImageSelectorItem> items = new ArrayList<>();
        items.add(ImageSelectorItem.newAddImageItem());
        setData(items);
    }

    public List<ImageSelectorItem> getItems(){
        return mData;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        ImageSelectorItem item = getItem(position);
        if (item.isAddFlag()) {
            return ITEM_ADD;
        } else {
            return ITEM_IMAGE;
        }
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch (itemType) {
            case ITEM_ADD:
                return new ImageSelectorAddHolder(mInflater.inflate(R.layout.item_image_selector_add, parent, false));
            default:
                return new ImageSelectorHolder(mInflater.inflate(R.layout.item_image_selector, parent, false));
        }
    }

    private class ImageSelectorAddHolder extends AbsViewHolder<ImageSelectorItem> {
        private ItemImageSelectorAddBinding mItemBinding;

        public ImageSelectorAddHolder(View convertView) {
            super(convertView);
            mItemBinding = DataBindingUtil.bind(convertView);
            ViewGroup.LayoutParams params = mItemBinding.flImage.getLayoutParams();
            if (params == null) {
                params = new ViewGroup.LayoutParams(mItemWidth, mItemWidth);
            } else {
                params.width = mItemWidth;
                params.height = mItemWidth;
            }
            mItemBinding.flImage.setLayoutParams(params);
        }

        @Override
        protected void onBindView(ImageSelectorItem item) {

        }
    }

    private class ImageSelectorHolder extends AbsViewHolder<ImageSelectorItem> {
        private ItemImageSelectorBinding mItemBinding;

        public ImageSelectorHolder(View convertView) {
            super(convertView);
            mItemBinding = DataBindingUtil.bind(convertView);
            ViewGroup.LayoutParams params = mItemBinding.flImage.getLayoutParams();
            if (params == null) {
                params = new ViewGroup.LayoutParams(mItemWidth, mItemWidth);
            } else {
                params.width = mItemWidth;
                params.height = mItemWidth;
            }
            mItemBinding.flImage.setLayoutParams(params);
        }

        @Override
        protected void onBindView(ImageSelectorItem item) {
            ImageLoader.getInstance().displayImage(StringUtils.filterNullAndTrim(item.imageUrl), mItemBinding.ivImage);
        }
    }


}
